//
//  IGTopicCommentTableViewCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicCommentTableViewCell.h"
#import "IntroViewController.h"
#import "IGTopicDetailVC.h"

@interface IGTopicCommentTableViewCell ()

@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) NSMutableAttributedString *commentAttributedText;///< 评论富文本信息

@end

@implementation IGTopicCommentTableViewCell

+ (IGTopicCommentTableViewCell *)cellWithTableView:(UITableView *)tableView {
    IGTopicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicCommentTableViewCell];
    if (!cell) {
        cell = [[IGTopicCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicCommentTableViewCell];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // 评论内容
        self.contentLabel = [[YYLabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 4 * kGAP10 - 2*kGAP10;
        self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.displaysAsynchronously = YES;
        self.contentLabel.textColor = [UIColor c4Color];
        self.contentLabel.font = [UIFont font13];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP15);
            make.right.mas_equalTo(-1 * kGAP10);
            make.top.mas_equalTo(self.contentView).offset(5.0);
        }];
        self.backgroundColor = [UIColor clearColor];
        self.hyb_lastViewInCell = self.contentLabel;
        self.hyb_bottomOffsetToCell = 3.0;
    }
    
    return self;
}

- (void)configCellWithModel:(IGCommentBean *)model {
    //清空
    self.commentAttributedText = [[NSMutableAttributedString alloc] initWithString:@""];
    
    UIFont *font = [UIFont font13];
    UIColor *color = [UIColor c5Color];
    if (![model.parentCommentUserNick isEqualToString:@""]) {//二级评论
//        NSMutableAttributedString *commentUserName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@回复", model.commentUserNickName] attributes:@{NSFontAttributeName: font}];
        NSMutableAttributedString *commentUserName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ ", model.commentUserNick,NSLocalizedString(@"Reply", nil)] attributes:@{NSFontAttributeName: font}];

        [commentUserName yy_setTextHighlightRange:NSMakeRange(0, model.commentUserNick.length) color:color backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = model.commentUserId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        [self.commentAttributedText appendAttributedString:commentUserName];
        
        NSMutableAttributedString *commentByUserName = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@:%@", model.parentCommentUserNick, model.content] attributes:@{NSFontAttributeName: font}];
        [commentByUserName yy_setTextHighlightRange:NSMakeRange(0, model.parentCommentUserNick.length) color:color backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = model.parentCommentUserId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        [self.commentAttributedText appendAttributedString:commentByUserName];
    } else {//一级评论
        NSMutableAttributedString *commentUserName = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@:%@", model.commentUserNick, model.content] attributes:@{NSFontAttributeName: font}];
        [commentUserName yy_setTextHighlightRange:NSMakeRange(0, model.commentUserNick.length) color:color backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = model.commentUserId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        [self.commentAttributedText appendAttributedString:commentUserName];
    }
    
    self.commentAttributedText.yy_lineSpacing = 5;
    self.commentAttributedText.yy_kern = [NSNumber numberWithFloat:.5];
    self.contentLabel.attributedText = self.commentAttributedText;
}

- (void)configShowMoreComment {
    self.commentAttributedText = [[NSMutableAttributedString alloc] initWithString:@""];
//    self.contentLabel.text = @"更多评论";
    self.contentLabel.text = NSLocalizedString(@"More Comments", nil);
    self.contentLabel.textColor = [UIColor c5Color];
    self.contentLabel.font = [UIFont font13];
}

- (NSMutableAttributedString *)commentAttributedText {
    if (!_commentAttributedText) {
        _commentAttributedText = [[NSMutableAttributedString alloc] init];
    }
    return _commentAttributedText;
}

@end
